package birdry.media
{
	import fl.video.FLVPlayback;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.external.ExternalInterface;
	
	/**
	* ...
	* @author 
	*/
	public class  FlvController extends Sprite	{
		
		public static const PROGRESS:String = "progress";
		public static const PLAY:String = "play";
		public static const STOP:String = "stop";
		public static const PAUSE:String = "pause";
		public static const STATE_CHANGE:String = "stage_change";
		public static const COMPLETE:String = "complete";
		public static const METADATA_RECEIVED:String = "metadataReceived";
		public static const BUFFERING_STATEENTERED:String = "bufferingStateEntered";
		
		public var totalTime:Number;
		public var playheadTime:Number;
		public var playheadPercentage:Number;
		public var state:String;
		
		var flvPath:String;
		var flvPlayback:FLVPlayback;
		var _mute:Boolean;
		var _volume:Number = 1;
		var autoReplay:Boolean;
		
		public function FlvController(flvPlayback:FLVPlayback, flvPath:String = "", autoReplay:Boolean = false ): void	{
			
			this.flvPlayback = flvPlayback;
			this.flvPath = flvPath;
			this.autoReplay = autoReplay;
			setLayout();
			addEvent();
		}
		
		public function play(str:String = ""): void	{
			//	if (str != "")		flvPlayback.source = str;
			if (str != "")		flvPlayback.play(str);
			flvPlayback.play();
		}
		
		public function stop(): void	{
			flvPlayback.pause();
			flvPlayback.seek(0);
			playheadTime = 0;
			playheadPercentage = 0;
			dispatchEvent( new Event( FlvController.PROGRESS ) );
			state = "stopped";
			dispatchEvent( new Event( FlvController.STATE_CHANGE ) );
		}
		public function resume(): void	{
			flvPlayback.play();
		}
		public function pause(): void	{
			flvPlayback.pause();
		}
		public function seek(time:Number): void	{
			flvPlayback.seek(time);
			playheadTime = time;
			playheadPercentage = time/totalTime*100;
			dispatchEvent( new Event( FlvController.PROGRESS ) );
		}
		public function seekPercent(per:Number): void	{
			flvPlayback.seekPercent(per);
			playheadTime = totalTime*per/100;
			playheadPercentage = per;
			dispatchEvent( new Event( FlvController.PROGRESS ) );
		}
		
		public function get mute(): Boolean	{
			return _mute;
		}
		public function set mute( value: Boolean ): void	{
			_mute = value;
			if (_mute)	{
				flvPlayback.volume = 0;
			}	else	{
				flvPlayback.volume = volume;
			}
		}
		
		public function get volume(): Number	{
			return _volume;
		}
		public function set volume( value: Number ): void	{
			_volume = value;
			flvPlayback.volume = value;
		}
		
		public function get source(): String	{
			return flvPlayback.source;
		}
		public function set source( value: String ): void	{
			flvPlayback.source = value;
			flvPlayback.pause();
		}
		
		private function setLayout(): void	{
			if(flvPath)	flvPlayback.source = flvPath;
		}
		
		private function onEnter( e: Event ): void	{
			playheadTime = flvPlayback.playheadTime;
			playheadPercentage = flvPlayback.playheadPercentage;
			dispatchEvent( new Event( FlvController.PROGRESS ) );
		}
		
		private function addEvent(): void	{
			flvPlayback.addEventListener("playingStateEntered", onPlayingStateEntered);
			flvPlayback.addEventListener("stoppedStateEntered", onStoppedStateEntered);
			flvPlayback.addEventListener("pausedStateEntered", onPausedStateEntered);
			flvPlayback.addEventListener("metadataReceived", onMetadataReceived);
			flvPlayback.addEventListener("complete", onComplete);
			flvPlayback.addEventListener("bufferingStateEntered", onBufferingStateEntered);
			
			
			function onPlayingStateEntered( e :Event ): void	{
				state = "playing";
				addEventListener(Event.ENTER_FRAME, onEnter);
				dispatchEvent( new Event( FlvController.STATE_CHANGE ) );
			}
			function onStoppedStateEntered( e :Event ): void	{
				state = "stopped";
				removeEventListener(Event.ENTER_FRAME, onEnter);
				dispatchEvent( new Event( FlvController.STATE_CHANGE ) );
			}
			function onPausedStateEntered( e :Event ): void	{
				state = "paused";
				removeEventListener(Event.ENTER_FRAME, onEnter);
				dispatchEvent( new Event( FlvController.STATE_CHANGE ) );
			}
			function onMetadataReceived( e: Event ): void	{
				totalTime = flvPlayback.totalTime;
				dispatchEvent( new Event( FlvController.METADATA_RECEIVED ) );
			}
			function onComplete( e: Event ): void	{
				if (autoReplay)	flvPlayback.play();
				playheadTime = flvPlayback.playheadTime;
				playheadPercentage = flvPlayback.playheadPercentage;
				dispatchEvent( new Event( FlvController.PROGRESS ) );
				dispatchEvent( new Event( FlvController.COMPLETE ) );
			}
			function onBufferingStateEntered( e: Event ): void	{
				dispatchEvent( new Event( FlvController.BUFFERING_STATEENTERED ) );
			}
		}
		
	}	// class
	
}	// package